package nacgraphs;

import edu.uci.ics.jung.graph.DirectedSparseMultigraph;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public final class GraphView extends DirectedSparseMultigraph<GraphVertex, GraphEdge>
{
    private DataManager dataManager;
    private HashMap<Integer, GraphVertex> graphVertexes;
    private HashMap<String, GraphEdge> graphEdges;
    
    public GraphView()
    {
        dataManager = new DataManager();
        graphVertexes = new HashMap<Integer, GraphVertex>();
        graphEdges = new HashMap<String, GraphEdge>();
        
        ArrayList<Student> allStudents = dataManager.getAllStudents();
        
        for(Student student : allStudents)
        {
            graphVertexes.put(student.getRm(), new GraphVertex(student));
        }
        
        build();
    }
    
    public HashMap<Integer, GraphVertex> getGraphVertexes()
    {
        return graphVertexes;
    }
    
    public HashMap<String, GraphEdge> getGraphEdges()
    {
        return graphEdges;
    }
    
    private void build()
    {
        for(Map.Entry<Integer, GraphVertex> entry : graphVertexes.entrySet())
        {
            GraphVertex graphVertex = entry.getValue();

            addVertex(graphVertex);
            
            for(int friendRm : graphVertex.getFriendsRms())
            {
                GraphEdge graphEdge = new GraphEdge(graphVertex, graphVertexes.get(friendRm));
                
                addEdge(graphEdge, graphVertexes.get(graphVertex.getRm()), graphVertexes.get(friendRm));
                
                graphEdges.put(generateEdgeKey(graphEdge.getSourceStudent().getRm(), graphEdge.getTargetStudent().getRm()), graphEdge);
            }
        }
    }
    
    public String generateEdgeKey(int sourceRm, int targetRm)
    {
        return "" + sourceRm + "" + targetRm;
    }
}
